package com.zntg.utils.file;

import com.zntg.bo.CrmAllFileBo;
import com.zntg.utils.DateUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TakeFilePathAndName {


    public static void main(String[] args) {
        // This is the path where the file's name you want to take.
        String path = "D:\\data";
       // getFile(path);
        ArrayList<File> files=getListFiles(path);
        System.out.println("list.size================="+files.get(0).getPath()+","+files.get(0).getName());
    }

    private static void getFile(String path) {
        // get file list where the path has
        File file = new File(path);
        // get the folder list
        File[] array = file.listFiles();
        List<CrmAllFileBo> list=new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            if (array[i].isFile()) {

                // only take file name
                CrmAllFileBo crmAllFileBo=new CrmAllFileBo();
                System.out.println("^^^^^" + array[i].getName());
                crmAllFileBo.setFileName(array[i].getName());
                // take file path and name
                System.out.println("#####" + array[i]);
                // take file path and name
                System.out.println("*****" + array[i].getPath());
                crmAllFileBo.setFileUrl(array[i].getPath());
                crmAllFileBo.setGetDate(DateUtils.getCurrentDate());
                list.add(crmAllFileBo);
            } else if (array[i].isDirectory()) {
                getFile(array[i].getPath());
            }
        }
    }
    /***
     * 获取指定目录下的所有的文件（不包括文件夹），采用了递归
     *
     * @param obj
     * @return
     */
    public static ArrayList<File> getListFiles(Object obj) {
        File directory = null;
        if (obj instanceof File) {
            directory = (File) obj;
        } else {
            directory = new File(obj.toString());
        }
        ArrayList<File> files = new ArrayList<File>();
        if (directory.isFile()) {
            files.add(directory);
            return files;
        } else if (directory.isDirectory()) {
            File[] fileArr = directory.listFiles();
            for (int i = 0; i < fileArr.length; i++) {
                File fileOne = fileArr[i];
                files.addAll(getListFiles(fileOne));
            }
        }
        return files;
    }


}